<?php
require_once('../../config.php');
Doctrine::loadModels('../../models');

// can combine agent and people into query to return one result set.
// wrote SQL select as unsure of doctrine method when multiple references to same table
if ($_REQUEST["agent_id"]) 
{
	$agent_id = strip_tags($_REQUEST["agent_id"]);
	//file_put_contents  ( "param_id.txt", $agent_id);	
	
	$query = $db_query = sprintf("SELECT a.agent_id, a.name_short, a.name_full, 
		a.agent_type_id, a.date_start, a.date_end, a.created, a.modified,
        o.purpose_id, o.gbif_status_id, o.email, o.phone, o.fax, o.website,
        p.item_name purpose, s.item_name gbif_status
	FROM organisations o
  		INNER JOIN agent a ON o.agent_org_id = a.agent_id
  		INNER JOIN lk_glossary p ON o.purpose_id = p.item_id
  		INNER JOIN lk_glossary s ON o.gbif_status_id = s.item_id 
	WHERE a.agent_id ='".$agent_id."'");
	
	$items = mysql_query($query);	
}

$dom = new DomDocument('1.0',"UTF-8");
$dom->formatOutput = true;
$root = $dom->createElement( "results" );
$dom->appendChild( $root );
	while(($item = mysql_fetch_assoc($items)))
	{
		$dn = $dom->createElement( "record" );
		$dn->setAttribute( 'agent_id', ($item['agent_id']));
		$dn->setAttribute( 'name_short', ($item['name_short']));
		$dn->setAttribute( 'name_full', ($item['name_full']));
		$dn->setAttribute( 'date_start', $item['date_start'] );
		$dn->setAttribute( 'date_end', $item['date_end'] );
		$dn->setAttribute( 'purpose_id', $item['purpose_id'] );
		$dn->setAttribute( 'purpose', $item['purpose'] );
		$dn->setAttribute( 'gbif_status_id', $item['gbif_status_id'] );
		$dn->setAttribute( 'gbif_status', $item['gbif_status'] );
		$dn->setAttribute( 'email', $item['email'] );
		$dn->setAttribute( 'phone', ($item['phone']));
		$dn->setAttribute( 'fax', ($item['fax']));
		$dn->setAttribute( 'website', ($item['website']));
		$dn->setAttribute( 'created', ($item['created']));
		$dn->setAttribute( 'modified', ($item['modified']));
		$root->appendChild( $dn );
	}		
header( "Content-type: text/xml; charset=UTF-8");
echo $dom->saveXML();

?>